// Playground - noun: a place where people can play

import UIKit

//for-in
//for
//while
//do-while

for i in -99..<99
{
    i*i
}

let str = "Hello, World!"
for c in str
{
    println(c)
}

let coursesArray = ["A","B","C"]
for item in coursesArray
{
    println(item)
}

for (index, item) in enumerate(coursesArray)
{
    println("\(index) is \(item)")
}

let coursesDict = [10:"AA", 11:"BB", 12:"CC"]
for (key, value) in coursesDict
{
    println("Dict :\(key) is \(value)")
}

let base = 2
let power = 10
var result = 1

for _ in 1...power
{
    result *= base
}

result

for var i = -100 ; i <= 100 ; i++
{
    i*i
}

for var i = -6.28 ; i <= 6.28 ; i++
{
    sin(i)
}

//二分查找法
var arr:[Int] = []
for var i = 0 ; i < 100 ; i++
{
    arr.append( Int(arc4random()%10000))
}

arr.sort(<)
arr

var min_num = 0 , max_num = arr.count
var mid_num:Int
var target = Int( arc4random() % 1000 )

while min_num < max_num
{
    mid_num = ( min_num + max_num ) / 2
    arr[mid_num]
    
    if arr[mid_num] == target
    {
        println("\(target) is found in index \(mid_num)")
        break
    }
    else if arr[mid_num] > target
    {
        max_num = mid_num - 1
    }
    else 
    {
        min_num = mid_num + 1
    }
}

if min_num >= max_num
{
    println("Cannot found \(target) in arr")
}











